package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    
    @RequestMapping("/view")
    public String role(Model model){
        return "system/role";
    }

    @RequiresPermissions(value = {"role:query", "角色列表"}, logical = Logical.OR)
    @RequestMapping("/query")
    @ResponseBody
    public Object query(QueryObject qo){
        return roleService.query(qo);
    }

    // 验证用户名
    @RequestMapping("/validateName")
    @ResponseBody
    public Object validateName(String name, Long id){
        return  roleService.validateUsernname(name, id);
    }

    @RequiresPermissions(value = {"role:save", "角色新增/编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Role entity){
        try {
            roleService.saveOrUpdate(entity);
        } catch (Exception ex) {
            return new JsonResult().mark(ex.getMessage());
        }
        return new JsonResult();
    }

    @RequiresPermissions(value = {"role:del", "角色删除"}, logical = Logical.OR)
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Long id) {
        int row = roleService.delete(id);
        return row > 0 ? new JsonResult() : new JsonResult().mark("没有匹配到id="+ id + "，的角色");
    }
    
    @RequestMapping("/selectByEmployeeId")
    @ResponseBody
    public Object selectByEmployeeId(Long id){
        return roleService.selectByEmployeeId(id);
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public Object selectAll(){
        return roleService.listAll();
    }

}
